package com.ezops.poc.transformer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ezops.poc.bo.ClientBaseTradeFileModel;
import com.ezops.poc.bo.ClientBondFileModel;
import com.ezops.poc.bo.ClientEquityFileModel;
import com.ezops.poc.bo.EzopsBondModel;
import com.ezops.poc.bo.EzopsEquityModel;
import com.ezops.poc.bo.EzopsModel;
import com.ezops.poc.bo.EzopsOTEquityModel;
import com.ezops.poc.bo.ot.ClientOTEquityModel;
import com.ezops.poc.util.ClientEzopsMapping;

/**
 * This class converts the incoming data into EZOPS format. TODO : This class
 * requires changes as it breaks OCP principle. Should use strategy pattern.
 */
@Component(value = "transformationBean")
public class EzopsFormatConverter {

	@Autowired
	ClientEzopsMapping mapper;

	private final static Log log = LogFactory
			.getLog(EzopsFormatConverter.class);

	public EzopsModel convert(ClientBaseTradeFileModel data) {
		if (data instanceof ClientBondFileModel) {
			return convert((ClientBondFileModel) data);
		} else if (data instanceof ClientEquityFileModel) {
			return convert((ClientEquityFileModel) data);
		} else
			return convert((ClientOTEquityModel) data);
	}
	
	public ClientBaseTradeFileModel reverseConvert(EzopsModel data) {
		if (data instanceof EzopsBondModel) {
			return reverseConvert((EzopsBondModel) data);
		} else if (data instanceof EzopsEquityModel) {
			return reverseConvert((EzopsEquityModel) data);
		} else
			return reverseConvert((EzopsOTEquityModel) data);
	}

	/**
	 * Convert the Bond client model into Ezops Model.
	 * 
	 * @param data
	 * @return
	 */
	private EzopsBondModel convert(ClientBondFileModel data) {
		EzopsBondModel model = new EzopsBondModel();
		mapper.convert(data, model);
		return model;
	}

	/**
	 * Convert the equity client model to Ezops Format.
	 * 
	 * @param data
	 * @return
	 */
	private EzopsEquityModel convert(ClientEquityFileModel data) {
		EzopsEquityModel model = new EzopsEquityModel();
		mapper.convert(data, model);
		return model;
	}

	/**
	 * Convert Ezops Bond model to client bond model.
	 * 
	 * @param data
	 * @return
	 */
	private ClientBondFileModel reverseConvert(EzopsBondModel data) {
		ClientBondFileModel model = new ClientBondFileModel();
		mapper.convert(data, model);
		return model;
	}

	/**
	 * Convert the Ezops equity model to client equity model.
	 * 
	 * @param data
	 * @return
	 */
	private ClientEquityFileModel reverseConvert(EzopsEquityModel data) {
		ClientEquityFileModel model = new ClientEquityFileModel();
		mapper.convert(data, model);
		return model;
	}

	private EzopsOTEquityModel convert(ClientOTEquityModel data) {
		EzopsOTEquityModel model = new EzopsOTEquityModel();
		mapper.convert(data, model);
		return model;
	}

	private ClientOTEquityModel reverseConvert(EzopsOTEquityModel data) {
		ClientOTEquityModel model = new ClientOTEquityModel();
		mapper.convert(data, model);
		return model;
	}
}